<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class Video_Article extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
            if( ! $this->session->userdata('admin_login')){
		redirect('/admin/login/' , 'refresh');
	    }
	    
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	    
	    /* check privilege */
	    $action = $this->uri->segment(4) ? $this->uri->segment(4) : 'view';
	    if( ! check_privilege($this->session->userdata['admin']['user_group_id'] , 'video_articles' , $action)){
		show_error( $this->lang->line('permission_denied') );
	    }
            
	    $this->template->set_template('backend');
            $this->load->model('video_article_model','video_article_model');
	}
	
	public function index()
	{
	    $order_by['create_date'] = 'DESC';
	    $this->video_article_model->set_order_by($order_by);
	    $data['video_article_list'] = $this->video_article_model->load_records(TRUE);
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','video_article_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','video_article_view',$data);
            $this->template->render();
	}
	
	public function add()
	{
	    $this->load->model('video_article_type_model','video_article_type_model');
	    $this->video_article_type_model->set_order_by('title');
	    $data['type_list'] = $this->video_article_type_model->load_records();
	    
	    $this->load->library('my_ckeditor');
	    $this->load->library('my_ckfinder');
	    $this->my_ckfinder->SetupCKEditorObject($this->my_ckeditor);
	    $this->my_ckeditor->ToolbarSet = 'Full';
	    
            $this->load->library('form_validation');
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','video_article_add_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','video_article_add_view',$data);
            $this->template->render();
	}
	
	public function add_process()
	{
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules('title_en', 'lang:title', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('title_th', 'lang:title', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('video_url', 'lang:video url', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('content_en', 'lang:content', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('content_en', 'lang:content', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('freindly_url', 'lang:freindly url', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('keyword', 'lang:keyword', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('description', 'lang:description', 'trim|xss_clean|required');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->add();
            }
            else
            {
		$admin_info = $this->session->userdata('admin');
                $this->load->helper('devlism');
                $input['article_video_type_id'] = $this->input->post('article_video_type');
                $input['student_group_id'] = 0;
		
		$title['en'] = $this->input->post('title_en');
		$title['th'] = $this->input->post('title_th');
                $input['title'] = json_encode($title);
		
		$content['en'] = $this->input->post('content_en');
		$content['th'] = $this->input->post('content_th');
                $input['content'] = json_encode($content);
		
		$input['video_url'] = $this->input->post('video_url');
                $input['url'] = generateSlug($this->input->post('freindly_url'));
                $input['keyword'] = $this->input->post('keyword');
                $input['description'] = $this->input->post('description');
                $input['create_by'] = $admin_info['id'];
                $input['create_date'] = time();
                $input['update_by'] = $admin_info['id'];
                $input['update_date'] = time();
                
                $this->video_article_model->insert_record($input);
                redirect('/admin/video_article/' , 'refresh');
            }
	}
    }